/* 
 * File:   XUDPServ.h
 * Author: minaev
 *
 * Created on 14 Октябрь 2013 г., 11:53
 */

#ifndef XUDPSERV_H
#define	XUDPSERV_H

#include <pthread.h>
//Berkeley sockets
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <netdb.h>

#include "XUDPListener.h"

class XUDPServ {
public:
    XUDPServ(int listenPort);
    virtual ~XUDPServ();
    int startListenThread();
    char* readMsg();
    int   readLastLen();
    uint32_t   readLastIP();
    int sendMsg(uint32_t ipaddr, uint16_t prt, const void* buf, int len);
    int getLostMessages();
    XUDPListener* setUDPListener(XUDPListener* listener);
    void setBroadcast(uint32_t ip);
    uint32_t getBroadcast();
private:
    static void* threadListenFunc(void* lnk);
    void addInBuffer(int len,char* buffer,uint32_t ip);
    int nextCursorWriteBuf();
    int nextCursorReadBuf();
    int nextCursorBuf(int oldcursor);
    void printWriteBuffer();
    char** inputBuffer;
    int*  inputBufferLens;
    uint32_t* inputBufferIp;
    int cursorWriteInputBuffer;
    int cursorReadInputBuffer;
    int lostMessages;
    uint32_t broadcastIP;
    
    int updSocket;
    int listenPort;
    struct sockaddr_in sa;
    struct sockaddr_in saoutUDP;
    XUDPListener* listener;
    pthread_t listenThread;
    pthread_mutex_t mutexAccessBuf;
};

#endif	/* XUDPSERV_H */

